package org.jwcloud.project.module.zx.zhuanxiang.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.jwcloud.project.module.zx.baseManager.service.repository.MatBaseManagerRepository;
import org.jwcloud.project.module.zx.common.FmtUtil;
import org.jwcloud.project.module.zx.common.Resp;
import org.jwcloud.project.module.zx.common.repository.RptCommonRepository;
import org.jwcloud.project.module.zx.common.service.RptUtService;
import org.jwcloud.project.module.zx.zhuanxiang.service.repository.MatZhuanxiangRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.jw.mid.mm.comm.common.controller.BaseController;
import com.jw.mid.mm.comm.common.controller.CrudController;
import com.jw.mid.mm.comm.common.json.Response;
import com.jw.mid.mm.comm.common.pagination.ListBounds;
import com.jw.mid.mm.comm.common.pagination.ListSlice;
import com.jw.mid.mm.comm.common.pagination.PageListSlice;
import com.jw.mid.mm.comm.common.pagination.Pagination;
import com.jw.mid.mm.comm.common.service.CrudService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
/**

drop view view_tip_all;

create view view_tip_all as
select t1.id,
	   t1.rel_base_id,
	   'process' process_type,
	   'tip' tip_type,
	   t1.process_code,
	   t1.tip_date,
	   f_date_add_playday(t1.start_date, t3.dur_time) should_done_date,
	   case when t1.go_state is null then 'no' else 'yes' end is_done,
	   t2.sq_type,
	   t1.done_date
  from mat_base_process_tip t1,
	   code_process_type t2,
	   mat_work t3
 where t1.is_tip = 1
   and t1.process_code = t2.v_code
   and t2.dur_time_rel = t3.id
 union all
select t1.id,
	   t1.rel_base_id,
	   'process' process_type,
	   'forst_warn' tip_type,
	   t1.process_code,
	   t1.tip_date,
	   f_date_add_playday(t1.start_date, t3.dur_time) should_done_date,
	   case when t1.go_state is null then 'no' else 'yes' end is_done,
	   t2.sq_type,
	   t1.done_date
  from mat_base_process_tip t1,
	   code_process_type t2,
	   mat_work t3
 where t1.is_first_warn = 1
   and t1.process_code = t2.v_code
   and t2.dur_time_rel = t3.id
 union all
select t1.id,
	   t1.rel_base_id,
	   'process' process_type,
	   'second_warn' tip_type,
	   t1.process_code,
	   t1.tip_date,
	   f_date_add_playday(t1.start_date, t3.dur_time) should_done_date,
	   case when t1.go_state is null then 'no' else 'yes' end is_done,
	   t2.sq_type,
	   t1.done_date
  from mat_base_process_tip t1,
	   code_process_type t2,
	   mat_work t3
 where t1.is_second_warn = 1 
   and t1.process_code = t2.v_code
   and t2.dur_time_rel = t3.id
 union all
 
select t1.id,
	   t1.rel_base_id,
	   t1.work_type,
	   t1.tip_type,
	   null process_code,
	   t1.create_date,
	   t1.should_done_date,
	   case when t1.work_type = 'sp' then
	   (select case when count(*) = count(case when vv1.sp_state != 0 then 1 else null end) then 'yes' else 'no' end
		  from mat_sp_log vv1 where concat(vv1.rel_base_id, vv1.type_flag) in (
		select concat(v1.rel_base_id, v1.type_flag) from mat_sp_log v1 where v1.id = t1.unique_sp_log_id
		))
		else 
		(select case when count(*) = count(case when vv1.bank_date is not null then 1 else null end) then 'yes' else 'no' end
		  from mat_sp_log vv1 where concat(vv1.rel_base_id, vv1.type_flag) in (
		select concat(v1.rel_base_id, v1.type_flag) from mat_sp_log v1 where v1.id = t1.unique_sp_log_id
		))
		end,
	   t1.sq_type,
	   case when t1.work_type = 'sp' then
	   (select case when count(*) = count(case when vv1.sp_state != 0 then 1 else null end) then max(vv1.sp_date) else null end
		  from mat_sp_log vv1 where concat(vv1.rel_base_id, vv1.type_flag) in (
		select concat(v1.rel_base_id, v1.type_flag) from mat_sp_log v1 where v1.id = t1.unique_sp_log_id
		))
		else 
		(select case when count(*) = count(case when vv1.bank_date is not null then 1 else null end) then max(vv1.bank_date) else null end
		  from mat_sp_log vv1 where concat(vv1.rel_base_id, vv1.type_flag) in (
		select concat(v1.rel_base_id, v1.type_flag) from mat_sp_log v1 where v1.id = t1.unique_sp_log_id
		))
		end
  from mat_sp_tip t1 
 
;
 * @author song
 *
 */
//@Api(value = "API - Controlle",
//description = "专项统计查询3个页面", tags = "baseManager")
//@RestController
//@Transactional
//@RequestMapping("/rest/mat/zhuanxiang")
public class MatZhuanxiangController extends BaseController{
	
	@Autowired
	public RptCommonRepository rptCommonRepository;
	
	@Autowired
	public MatBaseManagerRepository matBaseManagerRepository;
	
	@Autowired
	public MatZhuanxiangRepository matZhuanxiangRepository;
	
	@Autowired
    public RptUtService rptUtService;
	
	@RequestMapping("/spHis")
	public ModelAndView test(String param, ModelAndView model) {
		model.setViewName("test/zhuanxiang/spHis.html");
		return model;
	}
	
	@RequestMapping("/qujian")
	public ModelAndView qujian(String param, ModelAndView model) {
		model.setViewName("test/zhuanxiang/qujian.html");
		return model;
	}
	
	@GetMapping("querySpHisList")
    @ApiOperation(value = "历史审批记录", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "sbr_name", value = "申报人姓名", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "xqmc", value = "小区名称", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "sq_type", value = "申请类型  sq yfk js", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "sp_his_menu_name", value = "收卷登记 材料审核 材料三审 材料核准", required = false, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "sp_start_date_start", value = "申报时间 yyyy/mm/dd", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "sp_start_date_end", value = "申报时间 yyyy/mm/dd", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "sp_done_date_start", value = "审批时间 yyyy/mm/dd", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "sp_done_date_end", value = "审批时间 yyyy/mm/dd", required = false, dataType = "string", paramType = "query"),
    	
    	
    	@ApiImplicitParam(name = "get_name", value = "取件人姓名", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "get_state", value = "状态（取件）1已领取 0未领取", required = false, dataType = "string", paramType = "query"),
    })
    public Resp queryItemList(
    		@RequestParam(required = false) Map<String,String> param
    		){
		return Resp.success(this.rptUtService.pageBuild(
				() -> this.matZhuanxiangRepository.querySpHisList(param), param));
    }
	
	
	@PostMapping("doQujian")
    @ApiOperation(value = "取件", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "process_log_id", value = "process_log_id", required = true, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "get_name", value = "取件人姓名", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "get_id_card", value = "身份证号", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "get_phone", value = "联系电话", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "get_date", value = "取件时间 yyyy/mm/dd", required = true, dataType = "string", paramType = "query"),
    	
    })
    public Resp doQujian(
    		@RequestBody(required = false) Map<String, Object> param
    		){
		String flag = FmtUtil.nullRes(param
				, "process_log_id", "process_log_id"
				, "get_name", "取件人姓名"
				, "get_id_card", "身份证号"
				, "get_phone", "联系电话"
				, "get_date", "取件时间");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
		return Resp.success(this.matZhuanxiangRepository.doQujian(param));
    }
	
}